﻿using CommonEntity;
using ConfigurationCommon;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseCommon
{
    public class MongoDbClientScopedHelper
    {
        private readonly IMongoDatabase _database = null;

        public MongoClient _mongoClient;

        public MongoDbClientScopedHelper(ConfigurationScopeHelper config)
        {
            //var setting= MongoClientSettings.FromConnectionString("");
            
            _mongoClient = new MongoClient(config._gpMongodbConnectionString); 
        }

        public IMongoCollection<T> GetDBCollection<T>(string collectionName, string dbname)
        {
            if (_mongoClient != null)
            {
                var _database = _mongoClient.GetDatabase(dbname);
                if (_database != null)
                {
                    IMongoCollection<T> cc = _database.GetCollection<T>(collectionName);
                    return cc;
                }
            }
            return null;
        }

        public Page<T> GetPage<T>(int limit, int pageindex, FilterDefinition<T> tt, IMongoCollection<T> c)
        {
            return GetPage(limit, pageindex, tt, c, null);
        }

        public Page<T> GetPage<T>(int limit, int pageindex, FilterDefinition<T> tt, IMongoCollection<T> c, SortDefinition<T> sort)
        {
            Page<T> p = new Page<T>();
            p.Items = c.Find(tt).Sort(sort).Limit(limit).Skip((pageindex - 1) * limit).ToList();
            p.CurrentPage = pageindex;
            p.ItemsPerPage = limit;
            p.TotalItems = c.Find(tt).CountDocuments();
            p.TotalPage = p.TotalItems % p.ItemsPerPage == 0 ? p.TotalItems / p.ItemsPerPage : (p.TotalItems / p.ItemsPerPage + 1);
            return p;
        }

        public static IMongoCollection<T> GetDBCollectionStatic<T>(string collectionName, string dbname)
        {
            var client = new MongoClient(ConfigurationStaticHelper._gpMongodbConnectionString);
            if (client != null)
            {
                var _database = client.GetDatabase(dbname);
                if (_database != null)
                {
                    IMongoCollection<T> cc = _database.GetCollection<T>(collectionName);
                    return cc;
                }
            }
            return null;
        }

        public static Page<T> GetPageStaic<T>(int limit, int pageindex, FilterDefinition<T> tt, IMongoCollection<T> c)
        {
            return GetPageStaic(limit, pageindex, tt, c, null);
        }

        public static Page<T> GetPageStaic<T>(int limit, int pageindex, FilterDefinition<T> tt, IMongoCollection<T> c, SortDefinition<T> sort)
        {
            Page<T> p = new Page<T>();
            p.Items = c.Find(tt).Sort(sort).Limit(limit).Skip((pageindex - 1) * limit).ToList();
            p.CurrentPage = pageindex;
            p.ItemsPerPage = limit;
            p.TotalItems = c.Find(tt).CountDocuments();
            p.TotalPage = p.TotalItems % p.ItemsPerPage == 0 ? p.TotalItems / p.ItemsPerPage : (p.TotalItems / p.ItemsPerPage + 1);
            return p;
        }

        #region SELECT
        /// <summary>
        /// 根据查询条件，获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<T> GetList<T>(Expression<Func<T, bool>> conditions = null)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            if (conditions != null)
            {
                return collection.Find(conditions).ToList();
            }
            return collection.Find(_ => true).ToList();
        }
        #endregion

        #region INSERT/// <summary>
        /// 插入多条数据，数据用list表示
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<T> InsertMany<T>(List<T> list)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.InsertMany(list);
            return list;
        }
        #endregion
    }
}
